﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Reflection;

namespace Snake_Classic
{
    public class FoodAndBonus
    {
        private int x, y, BonusX, BonusY, width, height;
        private Rectangle FoodrectangleImage;
        private Rectangle BonusImage;
        private Wall wall;
        private Snake snake;

             
        public Rectangle bonusImage
        {
            get 
            {
                return BonusImage;
            }
            set 
            { 
                BonusImage = value;
            }
        }


        public Rectangle FoodRectangleImage
        {
            get 
            { 
                return FoodrectangleImage;
            }
            set 
            { 
                FoodrectangleImage = value;
            }
        }


        /// <summary>
        /// set food parameters, x coordinat, y coordinat, width and height
        /// </summary>
        /// <param name="randFood"></param>
        public FoodAndBonus(Snake snake, Wall wall)
        {
            this.snake = snake;
            this.wall = wall;
            Random randFood = new Random();
            //start location coordinats of food and bonus
            x = randFood.Next(0, 580);
            y = randFood.Next(0, 500);

            BonusX = randFood.Next(50, 550);
            BonusY = randFood.Next(50, 500);

            //food and bonus width and height
            width = 25;
            height = 25;
        }
        /// <summary>
        /// location food on a form
        /// </summary>
        /// <param name="randFood">random value</param>
        public void SetFoodLocation()
        {
            //create instance of class
            Form1 form1 = new Form1();
            Random randFood = new Random();
            //food x coordinal of location
            x = randFood.Next(50, form1.Width - 60);
            //food y coordinal of location
            y = randFood.Next(50, form1.Height - 120);
            for (int i = 0; i < snake.SnakeRec.Length; i++)
            {
                while ((x == wall.WallRec.X + 30) && (y == wall.WallRec.Y + 30) && (x == snake.SnakeRec[i].Location.X) && (y == snake.SnakeRec[i].Location.Y))
                {
                    x = randFood.Next(50, form1.Width - 60);
                    y = randFood.Next(50, form1.Height - 120);
                }
            }
        }

        public void SetBonusLocation()
        {
            //create instance of class
            Form1 form1 = new Form1();
            Random randFood = new Random();
            //x coordinat of bonus location
            BonusX = randFood.Next(50, form1.Width - 60);
            //y coordinat of bonus location
            BonusY = randFood.Next(50, form1.Height - 120);

            for (int i = 0; i < snake.SnakeRec.Length; i++)
            {
                while ((BonusX == wall.WallRec.X + 20) && (BonusY == wall.WallRec.Y + 30) && (BonusX == snake.SnakeRec[i].Location.X) && (BonusY == snake.SnakeRec[i].Location.Y))
                {
                    BonusX = randFood.Next(50, form1.Width - 60);
                    BonusY = randFood.Next(50, form1.Height - 120);
                }
            }
        }
        public void StartLocation()
        {
            BonusX = 1000;
            BonusY = 1000;
        }


        /// <summary>
        /// method for drawing food on a field
        /// </summary>
        /// <param name="graphics">parametr for drawing</param>
        public void DrawFood(Graphics graphics)
        {
            //path to the bonus image
            string directory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
            string Filepath = Path.Combine(directory, "apple.png");
            //create image from path
            Image foodRec = Image.FromFile(Filepath);
            FoodrectangleImage = new Rectangle(x, y, width, height);
            graphics.DrawImage(foodRec, FoodrectangleImage);
        }

        public void DrawBonus(Graphics graphics)
        {
            //path to the bonus image
            string directory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
            string Filepath = Path.Combine(directory, "bonus.png");
            //create image from path
            Image BonusRec = Image.FromFile(Filepath);
            //create rectangle for bonus image
            BonusImage = new Rectangle(BonusX, BonusY, width, height);
            //draw bonus on a form
            graphics.DrawImage(BonusRec, bonusImage);
        }
    }
}
